<style lang="less">
	.edidbox{
		color:#fff;
		width:110px;
		height:100%;
		.van-row{height:100%; text-align: center}
		.default{background: #E6A23C;height:100%; vertical-align: middle;}
		.delete{background: #F56C6C;height:100%;}
		.text{height: 20px; line-height: 20px; font-size: 12px; position: relative; top:50%; margin-top:-10px;}
	}
</style>
<template>
    <div class="addresslist" v-loading="loading">
		<div class="van-radio-group">
			<div v-for="item in data" :key="item.id" class="van-cell-group van-hairline--top-bottom">
				<van-swipe-cell :right-width="110">
					<div class="van-cell van-cell--clickable van-address-item">
						<div class="van-cell__value van-cell__value--alone" @click="change(item)">
							<van-checkbox class="van-radio__input" style="height:auto;" v-model="item.checked"></van-checkbox>
							<span class="van-radio__label">
								<div class="van-address-item__name">
									{{item.username}}，{{item.mobile}}
									<span class="van-tag van-tag--success">{{item.type == 0 ? '家庭' : '公司'}}</span>
									<span class="van-tag van-tag--danger" v-if="item.isdefault">默认</span>
								</div>
								<div class="van-address-item__address">{{item.regions_name + ' ' + item.address}}</div>
							</span>
						</div>
						<i class="van-icon van-icon-edit van-address-item__edit" @click="$router.push({name:'shop_updateaddress',params:{id:item.id}})"></i>
					</div>
					<div slot="right" class="edidbox">
						<van-row>
							<van-col span="12" class="default" @click.native="setdefault(item.id)">
								<div class="text">默认</div>
							</van-col>
							<van-col span="12" class="delete" @click.native="remove(item.id)">
								<div class="text">删除</div>
							</van-col>
						</van-row>
					</div>
				</van-swipe-cell>
			</div>
		</div>
		<button @click="$router.push({name:'shop_insertaddress'})" class="van-button van-button--danger van-button--large van-button--square van-address-list__add">
    		<span class="van-button__text">新增地址</span>
		</button>
	</div>
</template>

<script>
    import { mapState,mapActions } from 'vuex';
    import axios from 'axios';
    export default {
        name: "cart",
        data() {
            return {
                
            }
        },
        methods: {
			change(item){
				this.$store.commit('shop/defaultaddress',item.id);
				this.$router.push({name:'shop_payment'})
			},
			setdefault(id){
				this.$dialog.confirm({
                    title: '提示',
                    message: '确认设置该条地址为默认地址'
                }).then(() => {
                    this.$http.member.patch('vipaddress/'+id).then(res => {
                        if(!res.data.code){
                            this.$message.success('设置默认成功')
							this.$store.dispatch('shop/initaddress',true);
                        }else{
                            this.$message.error('设置默认失败,'+res.data.message);
                        }
                    })
                });
			},
			remove(id){
				this.$dialog.confirm({
                    title: '提示',
                    message: '确认删除该条地址信息'
                }).then(() => {
                    this.$http.member.delete('vipaddress/'+id).then(res => {
                        if(!res.data.code){
                            this.$message.success('删除成功')
							this.$store.dispatch('shop/initaddress',true);
                        }else{
                            this.$message.error('删除失败,'+res.data.message);
                        }
                    })
                });
			},
        },
        computed: {
            loading(){
                return this.$store.state.shop.addressloading;
            },
            data(){
                return this.$store.state.shop.address.map(m => {
					return Object.assign(m,{
						checked : m.id == this.$store.state.shop.defaultaddress
					})
				});
			}
        },
        mounted(){
            
        },
        created(){
            this.$store.dispatch('shop/initaddress');
        }
    }
</script>

